<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="活动发起人ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入活动发起人ID(组织发布活动时为0)" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="活动标题" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入活动标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="活动位置" prop="location">
        <el-input v-model="queryParams.location" placeholder="请输入活动位置" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="开始时间" prop="startDate">
        <el-date-picker clearable v-model="queryParams.startDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endDate">
        <el-date-picker clearable v-model="queryParams.endDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="组织id" prop="organizeId">
        <el-input v-model="queryParams.organizeId" placeholder="请输入组织id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:act:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:act:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:act:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="actList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动ID" align="center" prop="id" />
      <el-table-column label="活动标题" align="center" prop="name" />
      <el-table-column label="活动图片" align="center" prop="pic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pic" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="活动发起人ID" align="center" prop="id" />

      <el-table-column label="开始时间" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="活动内容" align="center" prop="content" /> -->
      <!-- <el-table-column label="活动地址" align="center" prop="address" /> -->
      <el-table-column label="活动位置" align="center" prop="location" />
      <!-- <el-table-column label="标签(最多3个)" align="center" prop="tag" /> -->
      <!-- 0-待审核 1-审核通过 2-审核未通过 -->
      <el-table-column label="审核状态" align="center" prop="reviewStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reviewStatus == 0" type="warning">待审核</el-tag>
          <el-tag v-if="scope.row.reviewStatus == 1" type="success">审核通过</el-tag>
          <el-tag v-if="scope.row.reviewStatus == 2" type="danger">审核未通过</el-tag>
        </template>
      </el-table-column>
      <!--  0-未开始 1-进行中 2-已结束 3-手动隐藏 -->
      <el-table-column label="活动状态" align="center" prop="actStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.actStatus == 0" type="warning">未开始</el-tag>
          <el-tag v-if="scope.row.actStatus == 1" type="success">进行中</el-tag>
          <el-tag v-if="scope.row.actStatus == 2" type="danger">已结束</el-tag>
        </template>
      </el-table-column>
      <!-- 1-已置顶 -->
      <el-table-column label="是否置顶 " align="center" prop="isTop">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isTop == 1" type="success">已置顶</el-tag>
          <el-tag v-if="scope.row.isTop == 0" type="warning">未置顶</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="隐藏 " align="center" prop="isHide">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isHide" @change="handleHide(scope.row)" :active-value="1" :inactive-value="0" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否为组织发布活动 1-组织发布的活动" align="center" prop="isOrganize" /> -->
      <!-- <el-table-column label="组织id" align="center" prop="organizeId" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['system:act:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-top" @click="handleTop(scope.row)"
            v-hasPermi="['system:act:edit']">置顶</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:act:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id(组织发布活动时为0)" />
        </el-form-item>
        <el-form-item label="活动标题" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动标题" />
        </el-form-item>
        <el-form-item label="活动内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="活动地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入活动地址" />
        </el-form-item>
        <el-form-item label="活动位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入活动位置" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startDate">
          <el-date-picker clearable v-model="form.startDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endDate">
          <el-date-picker clearable v-model="form.endDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动图片" prop="pic">
          <image-upload :limit="3" v-model="form.pic" />
        </el-form-item>
        <!-- <el-form-item label="组织id" prop="organizeId">
          <el-input v-model="form.organizeId" placeholder="请输入组织id" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 活动详情弹窗 -->
    <el-dialog title="审核活动详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-form ref="detailForm" :model="detailForm" label-width="120px">
        <el-form-item label="活动发起ID" prop="userId">
          <el-input v-model="detailForm.userId" disabled />
        </el-form-item>
        <el-form-item label="活动标题" prop="name">
          <el-input v-model="detailForm.name" disabled />
        </el-form-item>
        <el-form-item label="活动时间" prop="timeRange">
          <el-input v-model="detailForm.timeRange" disabled />
        </el-form-item>
        <el-form-item label="活动地点" prop="location">
          <el-input v-model="detailForm.location" disabled />
        </el-form-item>
        <el-form-item label="活动描述" prop="content">
          <el-input type="textarea" v-model="detailForm.content" :rows="8" disabled />
        </el-form-item>
        <el-form-item label="活动图片" prop="pic">
          <div class="image-preview-container">
            <template v-if="detailForm.pic && detailForm.picList && detailForm.picList.length > 0">
              <div class="image-item" v-for="(img, index) in detailForm.picList" :key="index">
                <image-preview :src="img" :width="100" :height="100" disabled />
              </div>
            </template>
            <div v-else class="image-placeholder">
              <i class="el-icon-picture-outline"></i>
              <i class="el-icon-zoom-in"></i>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="活动标签" prop="tag">
          <template v-if="detailForm.tag && detailForm.tagList && detailForm.tagList.length > 0">
            <el-tag v-for="tag in detailForm.tagList" :key="tag" style="margin-right: 8px;">
              {{ tag }}
            </el-tag>
          </template>
          <span v-else>暂无标签</span>
        </el-form-item>
        <el-form-item label="审核结果" prop="reviewStatus">
          <el-radio-group v-model="detailForm.reviewStatus">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">未通过</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleAudit">确 定</el-button>
        <el-button @click="detailOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAct, getAct, delAct, addAct, updateAct, auditAct, hideAct, topAct } from "@/api/system/act";

export default {
  name: "Act",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动表格数据
      actList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹窗
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        content: null,
        address: null,
        location: null,
        startDate: null,
        endDate: null,
        pic: null,
        tag: null,
        reviewStatus: null,
        actStatus: null,
        isTop: null,
        isOrganize: null,
        organizeId: null,
      },
      // 表单参数
      form: {},
      // 详情表单参数
      detailForm: {},
      // 表单校验
      rules: {
      },
      // imgList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询活动列表 */
    getList() {
      this.loading = true;
      listAct(this.queryParams).then(response => {
        this.actList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        name: null,
        content: null,
        address: null,
        location: null,
        startDate: null,
        endDate: null,
        pic: null,
        tag: null,
        reviewStatus: null,
        actStatus: null,
        isTop: null,
        isOrganize: null,
        organizeId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      // this.imgList = row.pic.split(',')
      // console.log(this.imgList, 'this.imgList')
      getAct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活动编号为"' + ids + '"的数据项？').then(function () {
        return delAct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('encounter/act/export', {
        ...this.queryParams
      }, `act_${new Date().getTime()}.xlsx`)
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.detailForm = { ...row };
      // 处理时间范围显示
      if (this.detailForm.startDate && this.detailForm.endDate) {
        const startDate = this.parseTime(this.detailForm.startDate, '{y}/{m}/{d} {h}:{i}');
        const endDate = this.parseTime(this.detailForm.endDate, '{y}/{m}/{d} {h}:{i}');
        this.detailForm.timeRange = `${startDate}-${endDate}`;
      }
      // 处理图片列表
      if (this.detailForm.pic) {
        this.detailForm.picList = this.detailForm.pic.split(',').filter(item => item.trim());
      }
      // 处理标签列表
      if (this.detailForm.tag) {
        this.detailForm.tagList = this.detailForm.tag.split(',').filter(item => item.trim());
      }
      this.detailOpen = true;
    },
    handleHide(row) {
      let isHide = row.isHide ? 1 : 0;
      this.$modal.confirm('是否确认隐藏活动编号为"' + row.id + '"的数据项？').then(function () {
        return hideAct(row.id, isHide);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => { });
    },
    handleAudit() {
      this.detailOpen = false;
      auditAct(this.detailForm.id, {
        reviewStatus: this.detailForm.reviewStatus
      }).then(res => {
        if (res.code == 200) {
          this.$modal.msgSuccess("审核成功");
          this.getList();
        } else {
          this.$modal.msgError(res.msg);
        }
      });
    },
    handleTop(row) {
      this.$modal.confirm('是否确认置顶活动编号为"' + row.id + '"的数据项？').then(function () {
        return topAct(row.id);
      }).then(res => {
        if (res.code == 200) {
          this.getList();
          this.$modal.msgSuccess("置顶成功");
        } else {
          this.$modal.msgError(res.msg);
        }
      }).catch(() => { });
    }
  }
};
</script>

<style scoped>
.image-preview-container {
  display: flex;
  gap: 10px;
}

.image-item {
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  overflow: hidden;
}

.image-placeholder {
  width: 100px;
  height: 100px;
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 20px;
}

.image-placeholder i {
  margin: 2px;
}
</style>
